package no.mehdi.usereg.rfid.controllers;

import java.io.Serializable;

import no.mehdi.usereg.rfid.exceptions.RFIDException;
import no.mehdi.usereg.rfid.listeners.AttachListenerApp;
import no.mehdi.usereg.rfid.listeners.DetachListenerApp;
import no.mehdi.usereg.rfid.listeners.ErrorListenerApp;
import no.mehdi.usereg.rfid.listeners.OutputChangeListenerApp;
import no.mehdi.usereg.rfid.listeners.TagGainListenerApp;
import no.mehdi.usereg.rfid.listeners.TagLossListenerApp;

import org.apache.log4j.Logger;

import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.RFIDPhidget;

public class RFIDController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	RFIDPhidget rfid;
	
	AttachListenerApp attachListener;
	
	DetachListenerApp detachListener;
	
	ErrorListenerApp errorListener;
	
	OutputChangeListenerApp outputChangeListener;
	
	TagGainListenerApp tagGainListener;
	
	TagLossListenerApp tagLossListener;
	
	Logger log = Logger.getLogger(this.getClass());

	public void startRfid() throws Exception {
		init();
		rfid.openAny();
		rfid.waitForAttachment(100000);
	}
	public void stopRfid() throws RFIDException {
		try {
			rfid.close();
			rfid = null;
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}

	public void init() throws RFIDException {
		try {
			if (log.isDebugEnabled()) {
				log.debug(Phidget.getLibraryVersion());
			}

			rfid = new RFIDPhidget();
			setListeners();
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}
	
	public void setListeners() throws RFIDException{
		//setAntennaOn();
		//setLedOn();
		rfid.addAttachListener(attachListener);
		if(log.isDebugEnabled()){
			log.debug("attachment of " + attachListener);
		}
		
		rfid.addDetachListener(detachListener);
		rfid.addErrorListener(errorListener);
		rfid.addOutputChangeListener(outputChangeListener);
		rfid.addTagGainListener(tagGainListener);
		rfid.addTagLossListener(tagLossListener);
		
	}
	
	public void setAntennaOn() throws RFIDException{
		try {
			rfid.setAntennaOn(true);
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}
	
	public void setAntennaOff() throws RFIDException{
		try {
			rfid.setAntennaOn(false);
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}
	public void setLedOn() throws RFIDException{
		try {
			rfid.setLEDOn(true);
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}
	public void setLedOff() throws RFIDException{
		try {
			rfid.setLEDOn(false);
		} catch (PhidgetException e) {
			throw new RFIDException(e.getMessage());
		} catch (Exception e) {
			throw new RFIDException(e.getMessage());
		}
	}
	public void setRfid(RFIDPhidget rfid) {
		this.rfid = rfid;
	}
	public void setAttachListener(AttachListenerApp attachListener) {
		this.attachListener = attachListener;
	}
	public void setDetachListener(DetachListenerApp detachListener) {
		this.detachListener = detachListener;
	}
	public void setErrorListener(ErrorListenerApp errorListener) {
		this.errorListener = errorListener;
	}
	public void setOutputChangeListener(OutputChangeListenerApp outputChangeListener) {
		this.outputChangeListener = outputChangeListener;
	}
	public void setTagGainListener(TagGainListenerApp tagGainListener) {
		this.tagGainListener = tagGainListener;
	}
	public void setTagLossListener(TagLossListenerApp tagLossListener) {
		this.tagLossListener = tagLossListener;
	}

}
